﻿using Sunny.UI;
using System;
using System.IO.Pipes;
using System.IO;
using System.Threading;
using CoreShop.App.Units;
using CoreShop.Library;
using CoreShop.Model.FromBody;
using CoreShop.Model;
using System.Collections.Generic;
using System.Windows.Forms;
using CoreShop.Services.Services.Report;
using System.Linq;
using static CoreShop.Library.GlobalEnumVars;
using Sunny.UI.Win32;
using CoreShop.Services;
using NPOI.Util;

namespace CoreShop.App
{
    public partial class fpgPaymentSum : UIPage
    {
        public int mPageIndex = 0;
        public fpgPaymentSum()
        {
            InitializeComponent();
            LoadComboxData();
        }

        private void LoadComboxData()
        {
            #region 加载支付方式
            this.cbmPayment2.Items.Clear();            
            var payList = new corecmspaymentsService().AsQueryable().Where(p => p.isEnable == true).ToList();
            cbmPayment2.DataSource = payList;
            cbmPayment2.DisplayMember = "name";
            cbmPayment2.ValueMember = "id";
            cbmPayment2.Text = "";
            cbmPayment2.SelectedValue = 0;
            #endregion
        }

        private void fpgPaymentSum_ReceiveParams(object sender, UIPageParamsArgs e)
        {
            mPageIndex = 1;
            var rptRequest = e.Value as PosReportRequest;
            if (rptRequest.type == (int)GlobalEnumVars.ReportRequest.Query)
            {
                LoadData(rptRequest);
            }
            else
            {
                ExportData();
            }
        }

        private void ExportData()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.Title = "Export Excel File To";
            saveFileDialog.ShowDialog();
            string fileName = saveFileDialog.FileName;
            if (fileName == "") return;
            Dictionary<string, DataGridView> gridViews = new Dictionary<string, DataGridView>();
            gridViews.Add("付款汇总", dgvDataList);
            if (ExportUtil.NPOIExportForDataGridview(gridViews, fileName))
            {
                MessageBox.Show("导出Excel成功！");
            }
        }


        /// <summary>
        ///销售统计
        /// </summary>
        private void LoadData(PosReportRequest posReport)
        {
            int count = 0;
            List<Reportscorecmsfoodorderpayment> lstSource = new corereportServices().GetOrderPaymentSum(ref count, mPageIndex, btnPg4.PageSize, posReport.starTime, posReport.endTime, 0, CommFunc.ConvertToInt32(cbmPayment2.SelectedValue));

            btnPg4.TotalCount = count;
            this.dgvDataList.AutoGenerateColumns = false;
            this.dgvDataList.DataSource = lstSource;
            //uiDataGridViewFooter1.DataGridView = sumRow;
            //设置统计绑定的表格
            dgvFooder.DataGridView = dgvDataList;
            dgvFooder["column4_orderNo"] = "合计：";
            dgvFooder["payAmount"] = CommFunc.ConvertToString(lstSource.Sum(p=>p.payAmount));
            dgvFooder["count"] = CommFunc.ConvertToString(lstSource.Sum(p => p.count));
            dgvFooder["refundCount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.refundCount));
            dgvFooder["refundAmount"] = CommFunc.ConvertToString(lstSource.Sum(p => p.refundAmount));
        }
    }
}